package com.boot.core.util

import com.alibaba.fastjson.JSON
import java.io.IOException
import javax.servlet.ServletOutputStream
import javax.servlet.http.HttpServletResponse

object AjaxUtil {
    /**
     * response方法
     *
     * @param response HttpServletResponse
     * @param obj   ResultEnum
     */
    fun outputMessage(response: HttpServletResponse, obj: Any) {
        val json = JSON.toJSONString(obj)
        response.characterEncoding = "UTF-8"
        response.contentType = "text/json"
        var servletOutputStream: ServletOutputStream? = null
        try {
            servletOutputStream = response.outputStream
            servletOutputStream!!.write(json.toByteArray(charset("utf-8")))
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            if (servletOutputStream != null) {
                try {
                    servletOutputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }
}